
const fs = require('fs');
const parser = require('@babel/parser');
const path = require('path');
const traverse = require('@babel/traverse').default;

function fenximokuai (name) {
  // const dependencies = [];
  const dependencies = {};
  const content = fs.readFileSync(name, 'utf-8');
  const ast = parser.parse(content, {
    sourceType: 'module',
  });
  // console.log(ast.program.body);
  traverse(ast, {
    ImportDeclaration ({node}) {
      // console.log(node);
      // dependencies.push(node.source.value);
      // 在项目中的路径
      const newFilename = './' + path.join(path.dirname(name), node.source.value);
      dependencies[node.source.value] = newFilename;
    }
  });
  console.log(dependencies);
}

fenximokuai('src/index.js');